package com.my.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.my.admin.entity.LeaveComment;
import com.my.admin.service.LeaveCommentService;
import com.my.admin.util.Rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
public class LeaveCommentController {

    @Autowired
    private LeaveCommentService service;

    @PostMapping("leaveComments/get")
    public Rest get(@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "10") int pageSize, @RequestBody LeaveComment comment) {
        return Rest.ok().setData("page", service.page(new Page<>(pageNo, pageSize), new QueryWrapper<LeaveComment>().
                like(StrUtil.isNotBlank(comment.getNickname()), "nickname", comment.getNickname()).
                like(StrUtil.isNotBlank(comment.getEmail()), "email", comment.getEmail()).
                like(StrUtil.isNotBlank(comment.getUrl()), "url", comment.getUrl())));
    }

    @DeleteMapping("leaveComments")
    public Rest del(@RequestParam Long id) {
        if(service.removeById(id)) {
            return Rest.ok("删除成功！");
        }
        return Rest.err("删除失败！");
    }

}
